<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主键" prop="linkageStrategyId">
        <el-input
          v-model="queryParams.linkageStrategyId"
          placeholder="请输入主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="策略名称" prop="linkageStrategyName">
        <el-input
          v-model="queryParams.linkageStrategyName"
          placeholder="请输入策略名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测点设备ID" prop="linkageDeviceId">
        <el-input
          v-model="queryParams.linkageDeviceId"
          placeholder="请输入测点设备ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测点设备名称" prop="linkageDeviceName">
        <el-input
          v-model="queryParams.linkageDeviceName"
          placeholder="请输入测点设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="告警级别" prop="linkageOperate">
        <el-input
          v-model="queryParams.linkageOperate"
          placeholder="请输入告警级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="告警值" prop="linkageOperateValue">
        <el-input
          v-model="queryParams.linkageOperateValue"
          placeholder="请输入告警值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createByName">
        <el-input
          v-model="queryParams.createByName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createAt">
        <el-date-picker clearable
          v-model="queryParams.createAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="updateByName">
        <el-input
          v-model="queryParams.updateByName"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="updateAt">
        <el-date-picker clearable
          v-model="queryParams.updateAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="版本" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段1" prop="extra1">
        <el-input
          v-model="queryParams.extra1"
          placeholder="请输入扩展字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段2" prop="extra2">
        <el-input
          v-model="queryParams.extra2"
          placeholder="请输入扩展字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段3" prop="extra3">
        <el-input
          v-model="queryParams.extra3"
          placeholder="请输入扩展字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="告警级别" prop="linkageAlarmValue">
        <el-input
          v-model="queryParams.linkageAlarmValue"
          placeholder="请输入告警级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址信息ID" prop="linkageAddressId">
        <el-input
          v-model="queryParams.linkageAddressId"
          placeholder="请输入地址信息ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址信息" prop="linkageAddress">
        <el-input
          v-model="queryParams.linkageAddress"
          placeholder="请输入地址信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否AI联动配置，默认0否，1是" prop="isAiLinkage">
        <el-input
          v-model="queryParams.isAiLinkage"
          placeholder="请输入是否AI联动配置，默认0否，1是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="AI联动配置联动配置编号" prop="linkageStrategyCode">
        <el-input
          v-model="queryParams.linkageStrategyCode"
          placeholder="请输入AI联动配置联动配置编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="AI联动配置联动告警类型编号" prop="alarmTypeCode">
        <el-input
          v-model="queryParams.alarmTypeCode"
          placeholder="请输入AI联动配置联动告警类型编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="AI联动配置是否启动应急" prop="isActivate">
        <el-input
          v-model="queryParams.isActivate"
          placeholder="请输入AI联动配置是否启动应急"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="灾害类型编号" prop="disasterTypeCode">
        <el-input
          v-model="queryParams.disasterTypeCode"
          placeholder="请输入灾害类型编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['linkage:strategy:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['linkage:strategy:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['linkage:strategy:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['linkage:strategy:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="strategyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="linkageStrategyId" />
      <el-table-column label="策略名称" align="center" prop="linkageStrategyName" />
      <el-table-column label="测点设备ID" align="center" prop="linkageDeviceId" />
      <el-table-column label="测点设备名称" align="center" prop="linkageDeviceName" />
      <el-table-column label="告警级别" align="center" prop="linkageOperate" />
      <el-table-column label="告警值" align="center" prop="linkageOperateValue" />
      <el-table-column label="策略状态" align="center" prop="linkageStatus" />
      <el-table-column label="创建人" align="center" prop="createByName" />
      <el-table-column label="创建时间" align="center" prop="createAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateByName" />
      <el-table-column label="修改时间" align="center" prop="updateAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="扩展字段1" align="center" prop="extra1" />
      <el-table-column label="扩展字段2" align="center" prop="extra2" />
      <el-table-column label="扩展字段3" align="center" prop="extra3" />
      <el-table-column label="告警级别" align="center" prop="linkageAlarmValue" />
      <el-table-column label="地址信息ID" align="center" prop="linkageAddressId" />
      <el-table-column label="地址信息" align="center" prop="linkageAddress" />
      <el-table-column label="是否AI联动配置，默认0否，1是" align="center" prop="isAiLinkage" />
      <el-table-column label="AI联动配置联动配置编号" align="center" prop="linkageStrategyCode" />
      <el-table-column label="AI联动配置联动告警类型编号" align="center" prop="alarmTypeCode" />
      <el-table-column label="AI联动配置联动告警类型" align="center" prop="alarmType" />
      <el-table-column label="AI联动配置是否启动应急" align="center" prop="isActivate" />
      <el-table-column label="灾害类型编号" align="center" prop="disasterTypeCode" />
      <el-table-column label="灾害类型" align="center" prop="disasterType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['linkage:strategy:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['linkage:strategy:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改联动策略对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主键" prop="linkageStrategyId">
          <el-input v-model="form.linkageStrategyId" placeholder="请输入主键" />
        </el-form-item>
        <el-form-item label="策略名称" prop="linkageStrategyName">
          <el-input v-model="form.linkageStrategyName" placeholder="请输入策略名称" />
        </el-form-item>
        <el-form-item label="测点设备ID" prop="linkageDeviceId">
          <el-input v-model="form.linkageDeviceId" placeholder="请输入测点设备ID" />
        </el-form-item>
        <el-form-item label="测点设备名称" prop="linkageDeviceName">
          <el-input v-model="form.linkageDeviceName" placeholder="请输入测点设备名称" />
        </el-form-item>
        <el-form-item label="告警级别" prop="linkageOperate">
          <el-input v-model="form.linkageOperate" placeholder="请输入告警级别" />
        </el-form-item>
        <el-form-item label="告警值" prop="linkageOperateValue">
          <el-input v-model="form.linkageOperateValue" placeholder="请输入告警值" />
        </el-form-item>
        <el-form-item label="创建人" prop="createByName">
          <el-input v-model="form.createByName" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createAt">
          <el-date-picker clearable
            v-model="form.createAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人" prop="updateByName">
          <el-input v-model="form.updateByName" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="修改时间" prop="updateAt">
          <el-date-picker clearable
            v-model="form.updateAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本" />
        </el-form-item>
        <el-form-item label="扩展字段1" prop="extra1">
          <el-input v-model="form.extra1" placeholder="请输入扩展字段1" />
        </el-form-item>
        <el-form-item label="扩展字段2" prop="extra2">
          <el-input v-model="form.extra2" placeholder="请输入扩展字段2" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="extra3">
          <el-input v-model="form.extra3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="告警级别" prop="linkageAlarmValue">
          <el-input v-model="form.linkageAlarmValue" placeholder="请输入告警级别" />
        </el-form-item>
        <el-form-item label="地址信息ID" prop="linkageAddressId">
          <el-input v-model="form.linkageAddressId" placeholder="请输入地址信息ID" />
        </el-form-item>
        <el-form-item label="地址信息" prop="linkageAddress">
          <el-input v-model="form.linkageAddress" placeholder="请输入地址信息" />
        </el-form-item>
        <el-form-item label="是否AI联动配置，默认0否，1是" prop="isAiLinkage">
          <el-input v-model="form.isAiLinkage" placeholder="请输入是否AI联动配置，默认0否，1是" />
        </el-form-item>
        <el-form-item label="AI联动配置联动配置编号" prop="linkageStrategyCode">
          <el-input v-model="form.linkageStrategyCode" placeholder="请输入AI联动配置联动配置编号" />
        </el-form-item>
        <el-form-item label="AI联动配置联动告警类型编号" prop="alarmTypeCode">
          <el-input v-model="form.alarmTypeCode" placeholder="请输入AI联动配置联动告警类型编号" />
        </el-form-item>
        <el-form-item label="AI联动配置是否启动应急" prop="isActivate">
          <el-input v-model="form.isActivate" placeholder="请输入AI联动配置是否启动应急" />
        </el-form-item>
        <el-form-item label="灾害类型编号" prop="disasterTypeCode">
          <el-input v-model="form.disasterTypeCode" placeholder="请输入灾害类型编号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStrategy, getStrategy, delStrategy, addStrategy, updateStrategy } from "@/api/linkage/strategy";

export default {
  name: "Strategy",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 联动策略表格数据
      strategyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        linkageStrategyId: null,
        linkageStrategyName: null,
        linkageDeviceId: null,
        linkageDeviceName: null,
        linkageOperate: null,
        linkageOperateValue: null,
        linkageStatus: null,
        createByName: null,
        createAt: null,
        updateByName: null,
        updateAt: null,
        version: null,
        status: null,
        extra1: null,
        extra2: null,
        extra3: null,
        linkageAlarmValue: null,
        linkageAddressId: null,
        linkageAddress: null,
        isAiLinkage: null,
        linkageStrategyCode: null,
        alarmTypeCode: null,
        alarmType: null,
        isActivate: null,
        disasterTypeCode: null,
        disasterType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询联动策略列表 */
    getList() {
      this.loading = true;
      listStrategy(this.queryParams).then(response => {
        this.strategyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        linkageStrategyId: null,
        linkageStrategyName: null,
        linkageDeviceId: null,
        linkageDeviceName: null,
        linkageOperate: null,
        linkageOperateValue: null,
        linkageStatus: null,
        createBy: null,
        createByName: null,
        createAt: null,
        updateBy: null,
        updateByName: null,
        updateAt: null,
        remark: null,
        version: null,
        status: null,
        extra1: null,
        extra2: null,
        extra3: null,
        linkageAlarmValue: null,
        linkageAddressId: null,
        linkageAddress: null,
        isAiLinkage: null,
        linkageStrategyCode: null,
        alarmTypeCode: null,
        alarmType: null,
        isActivate: null,
        disasterTypeCode: null,
        disasterType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.linkageStrategyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加联动策略";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const linkageStrategyId = row.linkageStrategyId || this.ids
      getStrategy(linkageStrategyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改联动策略";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.linkageStrategyId != null) {
            updateStrategy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStrategy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const linkageStrategyIds = row.linkageStrategyId || this.ids;
      this.$modal.confirm('是否确认删除联动策略编号为"' + linkageStrategyIds + '"的数据项？').then(function() {
        return delStrategy(linkageStrategyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('linkage/strategy/export', {
        ...this.queryParams
      }, `strategy_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
